package com.person.blog.config;

import com.person.blog.common.ErrorCode;
import com.person.blog.common.ResultBean;
import com.person.blog.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(BusineseException.class)
    public ResultBean businessException(BusineseException e) {
        log.error("businessException:" + e.getMessage(), e);
        return ResultUtil.error(ErrorCode.SYSTEM_ERROR, e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public ResultBean runtimeException(RuntimeException e) {
        log.error("runtimeException:" + e.getMessage(), e);
        e.printStackTrace();
        return ResultUtil.error(40101, e.getMessage(), e.getLocalizedMessage());
    }
}